package com.lei.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lei.eduservice.entity.EduChapter;
import com.lei.eduservice.entity.EduCourse;
import com.lei.eduservice.entity.EduTeacher;
import com.lei.eduservice.entity.chapter.ChapterVo;
import com.lei.eduservice.entity.vo.frontvo.CourseWebVo;
import com.lei.eduservice.entity.vo.frontvo.FCouseQueryVo;
import com.lei.eduservice.feign.OrderClient;
import com.lei.eduservice.service.EduChapterService;
import com.lei.eduservice.service.EduCourseService;
import com.lei.utils.JwtUtils;
import com.lei.utils.Result;
import com.lei.utils.vo.CourseWebVoOrder;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
@RestController
@RequestMapping("/edu/course/f")
//前台课程接口
@Api("前台课程接口")
public class FrontCourseController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    //分页查询
    @PostMapping("/getCourseList/{current}/{limit}")
    public Result getTeacherList(@PathVariable("current") Long current,
                                 @PathVariable("limit") Long limit,
                                 @RequestBody(required = false) FCouseQueryVo fCouseQueryVo){
        Page<EduCourse> coursePage = new Page<>(current, limit);
        HashMap<String,Object> map=courseService.getFrontCourseList(coursePage,fCouseQueryVo);
        return Result.ok().data(map);
    }

    //根据Id'获取课程信息和章节小节信息
    @GetMapping("/getCourseInfo/{courseId}")
    public Result getTeacherList(@PathVariable("courseId") String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);

        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        //远程调用订单接口,查询该用户是否已经购买
        boolean isBuy = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Result.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuy);
    }

    //根据Id获取课程信息 Order
    @GetMapping("/getCourseById/{courseId}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable("courseId") String courseId){
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
